export type singleTypeObject<T> = {
    [key: string]: T | T[];
};

/**
 * * 向 URLsearchParams Headers 等类型的数据输入对象的方式,但是不能对 FromData 使用
 */
export const setSingleTypeObject = <P extends URLSearchParams | Headers>(
    Package: P,
    /** 如果输入为数组则会获取多个值 */
    keyValuePairs: singleTypeObject<string>
): P => {
    Object.entries(keyValuePairs).forEach(([key, value]) => {
        if (value instanceof Array) {
            value.forEach((i) => {
                Package.append(key, i);
            });
        } else {
            Package.set(key, value);
        }
    });
    return Package;
};

/**
 * * 设置 FormData 的函数
 */
export const setFormData = (
    Package: FormData,
    /** 如果输入为数组则会获取多个值 */
    keyValuePairs: Object,
    append = false
) => {
    const func = Package[append ? "append" : "set"].bind(Package);
    Object.entries(keyValuePairs).forEach(([key, value]) => func(key, value));
    return Package;
};
